package com.example.springboot.service;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.entity.User;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.utils.TokenUtils;

import cn.hutool.core.util.StrUtil;

@Service
public class UserService extends ServiceImpl<UserMapper,User>{

    @Autowired
    UserMapper userMapper;

    @Override
    public boolean save(User entity) {
        if(StrUtil.isBlank(entity.getName())){
            entity.setName(entity.getCount());
        }
        if(StrUtil.isBlank(entity.getPwd())){
            entity.setPwd("123");
        }
        if(StrUtil.isBlank(entity.getRole())){
            entity.setRole("顾客");
        }
        return super.save(entity);
    }
    public User selectByCount(String count){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("count",count);
        return getOne(queryWrapper);
    }

    public List<User> selectByName(String name) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("name",name);
        return userMapper.selectList(queryWrapper);
    }
    
    public List<User> selectByRole(String role) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("role",role);
        return userMapper.selectList(queryWrapper);
    }

    public User login(User user) {
        User dbUser=selectByCount(user.getCount());
        if(dbUser == null){
            //抛出一个自定义的异常
            throw new ServiceException("账号或密码错误！");
        }
        if(dbUser.getRole() == null){
            //抛出一个自定义的异常
            throw new ServiceException("账号或密码错误！");
        }
        if(!dbUser.getRole().equals("店员") && !dbUser.getRole().equals("店长")){
            //抛出一个自定义的异常
            throw new ServiceException("账号或密码错误！");
        }
        if(!user.getPwd().equals(dbUser.getPwd())){
            throw new ServiceException("账号或密码错误！");
        }
        //生成token
        String token=TokenUtils.createToken(dbUser.getCount().toString(),dbUser.getPwd());
        dbUser.setToken(token);
        return dbUser;
    }

    public User registry(User user) {
        User dbUser=selectByCount(user.getCount());
        if(dbUser != null){
            //抛出一个自定义的异常
            throw new ServiceException("该账号已存在！");
        }
        user.setName(user.getCount());
        userMapper.insert(user);
        return user;
    }

    public void resetPassword(User user) {
        User dbUser=selectByCount(user.getCount());
        if(dbUser == null){
            //抛出一个自定义的异常
            throw new ServiceException("账号不存在！");
        }
        if(!user.getPhone().equals(dbUser.getPhone())){
            throw new ServiceException("验证错误！");
        }
        dbUser.setPwd("123");//重置密码
        updateById(dbUser);
    }
    
}
